package com.doudou.pay.helper.service.impl;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.doudou.pay.helper.commons.PayConstans;
import com.doudou.pay.helper.commons.PayRedisKey;
import com.doudou.pay.helper.entities.MerchantUser;
import com.doudou.pay.helper.entities.PayUser;
import com.doudou.pay.helper.mapper.MerchantUserMapper;
import com.doudou.pay.helper.service.AuthTokenService;
import com.doudou.pay.helper.service.PayUserService;

@Service("authTokenService")
public class AuthTokenServiceImpl implements AuthTokenService {

	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@Autowired
	private PayUserService payUserService;

	@Autowired
	private MerchantUserMapper merchantUserMapper;

	@Override
	public boolean isValidAuthToken(String authToken) {
		PayUser payUser = payUserService.getPayUserByAuthToken(authToken);
		if (null == payUser) {
			return false;
		}

		MerchantUser user = merchantUserMapper.selectByUserId(payUser
				.getUserId());
		if (null == user) {
			return false;
		}
		if (String.valueOf(PayConstans.USER_EFFECTIVE_TYPE_BALANCE).equals(
				user.getEffectivenessType())) {
			return user.getBalance() > 0;
		} else {
			if (payUser.getExpireDate().getTime() < System.currentTimeMillis()) {
				return false;
			}
		}
		return true;
	}

	@Override
	public boolean authentication(String authToken, String channelId) {
		try {
			if (this.isValidAuthToken(authToken)) {
				stringRedisTemplate.opsForValue().set(
						PayRedisKey.getAuthTokenKey(authToken), channelId, 5,
						TimeUnit.MINUTES);

				PayUser payUser = payUserService
						.getPayUserByAuthToken(authToken);
				String redisKey = PayRedisKey.getUserTokenHashKey(payUser
						.getUserId());
				stringRedisTemplate.opsForHash().put(redisKey, authToken, "0");
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public boolean isAuthtication(String authToken, String channelId) {
		if (stringRedisTemplate.hasKey(PayRedisKey.getAuthTokenKey(authToken))) {
			stringRedisTemplate.opsForValue().set(
					PayRedisKey.getAuthTokenKey(authToken), channelId, 5,
					TimeUnit.MINUTES);
			return true;
		}
		return false;
	}

	@Override
	public boolean deleteAuthentication(String authToken) {
		stringRedisTemplate.delete(PayRedisKey.getAuthTokenKey(authToken));

		PayUser payUser = payUserService.getPayUserByAuthToken(authToken);
		stringRedisTemplate.opsForHash()
				.delete(PayRedisKey.getUserTokenHashKey(payUser.getUserId()),
						authToken);
		return false;
	}

	@Override
	public boolean clientIsOnline(String authToken) {
		return stringRedisTemplate.hasKey(PayRedisKey
				.getAuthTokenKey(authToken));
	}

	@Override
	public String getChannelId(String authToken) {
		return stringRedisTemplate.opsForValue().get(
				PayRedisKey.getAuthTokenKey(authToken));
	}

}
